package com.firexxx.common.web;

import com.firexxx.common.errors.BusinessError;
import lombok.Getter;

import java.io.Serializable;

/**
 * Created by mmxu6 on 2019-09-03.
 */
@Getter
public class RestResponse<T> implements Serializable {
    private static final long serialVersionUID = -8209853144735356505L;

    private String code;
    private String message;
    private T data;

    private RestResponse() {
        super();
    }

    private RestResponse(T data) {
        this();
        this.data = data;
    }

    private RestResponse(String code, String message) {
        this();
        this.code = code;
        this.message = message;
    }

    private RestResponse(String code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static RestResponse<?> success() {
        return new RestResponse<>("0000", "成功");
    }

    public static RestResponse<?> error(String code, String message) {
        return new RestResponse<>(code, message);
    }

    public static <T> RestResponse<T> success(T result) {
        return new RestResponse<>("0000", "成功", result);
    }

    public static <E extends BusinessError> RestResponse<?> error(E error) {
        return error(error.getCode(), error.getMessage());
    }
}
